<?php

namespace MarketServer\Controller;

/**
 * 用户控制器
 *
 * @since 1.0 2014-6-13 by sutroon
 */
class AdminController extends AppbaseController {
    
    /**
     * 管理员列表页面
     * @since 1.0 <2014-6-13> SoChishun Added.
     * @since 2.0 <2015-6-12> SoChishun Refacting.
     */
    public function admin_list() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        D('Login')->refresh_online_state();
        $this->display();
    }

    public function admin_edit($id = 0) {
        if ($id) {
            $data = D('Admin')->find($id);
            $this->assign('data', $data);
        }
        $this->display();
    }
    
    public function admin_role($id){
        $this->assign('data',D('Admin')->find($id));
        $this->display();
    }

    /**
     * 获取列表查询数据
     * @return array
     * @since 1.0 <2015-7-2> SoChishun Added.
     */
    function get_admin_search_data() {
        $search = I();
        if ($search['create_time']) {
            $where['create_time'] = array('EGT', $search['create_time']);
        }
        if ($search['user_name']) {
            $where['user_name'] = $search['user_name'];
        }
        return array('search' => $search, 'where' => $where);
    }

    /**
     * 导出数据
     * @since 1.0 <2014-6-13> SoChishun Added.
     */
    public function admin_export() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        $search_data = $this->get_admin_search_data();
        $list = D('Admin')->scope('export')->where($search_data['where'])->select();
        if (!$list) {
            $this->ajaxMsg(false, '找不到符合条件的记录!');
        }
        sofn_excel_export($list);
    }

    /**
     * 获取列表JSON数据
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_admin_list_json() {
        $search_data = $this->get_admin_search_data();
        $list = D('Admin')->get_admin_list($search_data['where']);
        $this->ajaxReturn($list);
    }

    /**
     * 获取可用租户ID
     * @param int $max
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function get_siteid_json($max = 50) {
        $skips = M('tuser')->distinct(true)->getField('siteID', true);
        $out = array();
        for ($i = 1; $i < $max; $i++) {
            if ($skips && in_array($i, $skips)) {
                continue;
            }
            $out[] = array('id' => $i, 'text' => $i);
        }
        $this->ajaxReturn($out);
    }

    /**
     * 获取租户列表
     */
    public function get_admin_json() {
        $arr = D('Admin')->scope('idtext')->select();
        $this->ajaxReturn($arr);
    }

    public function admin_password_reset() {
        $id = I('id');
        $where['id'] = $id;
        if (!$id) {
            $this->ajaxMsg(false, '参数有误');
        }
        $enc=new \Org\So\SuEncrypt();        
        $result = D('Admin')->update_password($where, $enc->password('123456',C('USER.ENCRYPT_KEY')));
        $this->ajaxMsg($result, '密码重置{%}');
    }

    /**
     * 保存管理员
     * @since 1.0 <2015-6-11> SoChishun Added.
     */
    public function save_admin_json() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        $msg = D('Admin')->save_admin($this->user_login_data['user_name']);
        if (!$msg['status'] && is_array($msg['info'])) {
            $msg['info'] = implode("<br />", $msg['info']);
        }
        $this->ajaxReturn($msg);
    }

    /**
     * 保存管理员角色
     * @since 1.0 <2015-8-4> SoChishun Added.
     */
    public function save_admin_role() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        $id = I('id');
        $role_id = I('role_id');
        if(!$id || !$role_id){
            $this->ajaxMsg(false,'参数有误');
        }
        $result = M('tuser')->where(array('id' => $id))->setField('role_id', $role_id);
        $this->ajaxMsg($result);
    }

    /**
     * 删除管理员
     * @param string $id 主键编号,多个之间以逗号隔开
     * @since 1.0 <2015-6-10> SoChishun Added.
     */
    public function admin_delete($id = '') {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        if (!$id) {
            $this->ajaxMsg(false, '编号无效');
        }
        $msg = D('Admin')->admin_delete($id, $this->user_login_data['user_name']);
        $this->ajaxReturn($msg);
    }

    public function admin_recharge() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        $id = I('id');
        if (!$id) {
            $this->ajaxMsg(false, '编号有误!');
        }
        $data = M('tuser')->find($id);
        if (!$data) {
            $this->ajaxMsg(false, '用户不存在!');
        }
        $this->assign('data', $data);
        $this->display();
    }

    /**
     * 保存项目
     */
    public function admin_recharge_save() {
        if(!$this->validate_permission('YH_M3_GLYGL',$msg)){
            die($msg);
        }
        $amount = I('amount');
        $siteID = I('siteID');
        $params = new \Org\So\Params();
        $params->addParam('siteID', 'number', '用户账户无效!');
        $params->addParam('amount', 'double', '充值金额必须填写!');
        $params->addParam('origin', 'require', '充值来源必须选择!');
        if ($params->hasError()) {
            $this->ajaxMsg(false, $params->getError());
        }
        $origin = I('origin');
        $remark = I('remark');
        $params->clearParams();
        $params->addData('pflt_amount', $amount);
        $params->addData('pint_siteID', $siteID);
        $params->addData('pstr_origin', $origin);
        $params->addData('pstr_remark', $remark);
        $result = $params->query_procedure('proc_recharge');
        $result = mysql_fetch_array($result);
        $result = ($result && $result['vint_errno'] == '0') ? true : false;
        BaseController::add_user_event_log('充值', "充值{%}, 租户ID:$siteID, 金额: $amount, 支付方式: $origin", $result);
        if ($result) {
            M()->clear_table_caching(array('tuser', 'tfinance_recharge'));
            $this->ajaxMsg(true, '充值成功');
        } else {
            $this->ajaxMsg(false, '充值失败');
        }
    }

}
